![PyPI Now Supports iOS and Android Wheels for Mobile Python Development](https://cdn.sanity.io/images/cgdhsj6q/production/96416c872705517a6a65ad9646ce3e7caef623a0-1024x1024.webp?w=400&fit=max&auto=format)
Security News
PyPI Now Supports iOS and Android Wheels for Mobile Python Development
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
The sift npm package is a library that provides a way to use MongoDB-like query syntax for filtering, sorting, and manipulating JavaScript arrays and objects. It is commonly used for client-side querying of JSON data, in-memory database emulation, and other applications where MongoDB query semantics are desired without a database.
Filtering Arrays
This code sample demonstrates how to filter an array of objects to find elements where the age is greater than 18 and the name starts with the letter 'J'.
{"$and":[{"age":{"$gt":18}},{"name":{"$regex":"^J"}}]}
Sorting Arrays
This code sample shows how to sort an array of objects by the age property in ascending order.
{"$sort":{"age":1}}
Projection
This code sample illustrates how to create a projection of an array of objects, including only the name and age properties in the output.
{"$project":{"name":1,"age":1}}
The official MongoDB driver for Node.js. It allows you to work with MongoDB databases directly. While it provides similar querying capabilities, it is designed for server-side use with an actual MongoDB database rather than in-memory operations like sift.
An Object Data Modeling (ODM) library for MongoDB and Node.js. Mongoose provides a straight-forward, schema-based solution to model application data. It includes built-in type casting, validation, query building, and business logic hooks. It is more complex and feature-rich compared to sift, and it is intended for use with MongoDB databases.
A modern JavaScript utility library delivering modularity, performance, & extras. Lodash provides a wide range of functions for manipulating and querying data, but it does not use MongoDB-like query syntax. It is more general-purpose compared to sift.
Underscore is a utility-belt library for JavaScript that provides functional programming helpers without extending any built-in objects. It's similar to lodash and does not offer MongoDB-like query syntax, but it can be used for data manipulation and querying.
For extended documentation, checkout http://docs.mongodb.org/manual/reference/operator/query/
var sift = require('sift');
//intersecting arrays
var sifted = sift({ $in: ['hello','world'] }, ['hello','sifted','array!']); //['hello']
//regexp filter
var sifted = sift(/^j/, ['craig','john','jake']); //['john','jake']
//A *sifter* is returned if the second parameter is omitted
var testQuery = sift({
//you can also filter against functions
name: function(value) {
return value.length == 5;
}
});
//filtered: [{ name: 'craig' }]
[{
name: 'craig',
},
{
name: 'john'
},
{
name: 'jake'
}].filter(testQuery);
//you can test *single values* against your custom sifter
testQuery({ name: 'sarah' }); //true
testQuery({ name: 'tim' }); //false\
<html>
<head>
<script src="https://raw.github.com/crcn/sift.js/master/sift.min.js" type="text/javascript"></script>
<script type="text/javascript">
//regexp filter
var sifted = sift(/^j/, ['craig','john','jake']); //['john','jake']
</script>
</head>
<body>
</body>
</html>
filter
- the filter to use against the target arrayarray
- sifts against target array. Without this, a function is returnedselectorFn
- selector for the values within the array.With an array:
sift({$exists:true}, ['craig',null]); //['craig']
Without an array, a sifter is returned:
var siftExists = sift({$exists:true});
siftExists('craig'); //true
siftExists(null); //false
['craig',null].filter(siftExists); //['craig']
With a selector:
var sifter = sift({$exists:true}, function(user) {
return !!user.name;
});
sifter([
{
name: "Craig"
},
{
name: null
}
])
With your sifter, you can also test values:
siftExists(null); //false
siftExists('craig'); //true
See MongoDB's advanced queries for more info.
array value must be $in the given query:
Intersecting two arrays:
//filtered: ['Brazil']
sift({ $in: ['Costa Rica','Brazil'] }, ['Brazil','Haiti','Peru','Chile']);
Here's another example. This acts more like the $or operator:
sift({ location: { $in: ['Costa Rica','Brazil'] } }, [ { name: 'Craig', location: 'Brazil' } ]);
Opposite of $in:
//filtered: ['Haiti','Peru','Chile']
sift({ $nin: ['Costa Rica','Brazil'] }, ['Brazil','Haiti','Peru','Chile']);
Checks if whether a value exists:
//filtered: ['Craig','Tim']
sift({ $exists: true }, ['Craig',null,'Tim']);
You can also filter out values that don't exist
//filtered: [{ name: 'Craig', city: 'Minneapolis' }]
sift({ city: { $exists: false } }, [ { name: 'Craig', city: 'Minneapolis' }, { name: 'Tim' }]);
Checks if a number is >= value:
//filtered: [2, 3]
sift({ $gte: 2 }, [0, 1, 2, 3]);
Checks if a number is > value:
//filtered: [3]
sift({ $gt: 2 }, [0, 1, 2, 3]);
Checks if a number is <= value.
//filtered: [0, 1, 2]
sift({ $lte: 2 }, [0, 1, 2, 3]);
Checks if number is < value.
//filtered: [0, 1]
sift({ $lt: 2 }, [0, 1, 2, 3]);
Checks if query == value. Note that $eq can be omitted. For $eq, and $ne
//filtered: [{ state: 'MN' }]
sift({ state: {$eq: 'MN' }}, [{ state: 'MN' }, { state: 'CA' }, { state: 'WI' }]);
Or:
//filtered: [{ state: 'MN' }]
sift({ state: 'MN' }, [{ state: 'MN' }, { state: 'CA' }, { state: 'WI' }]);
Checks if query != value.
//filtered: [{ state: 'CA' }, { state: 'WI'}]
sift({ state: {$ne: 'MN' }}, [{ state: 'MN' }, { state: 'CA' }, { state: 'WI' }]);
Modulus:
//filtered: [300, 600]
sift({ $mod: [3, 0] }, [100, 200, 300, 400, 500, 600]);
values must match everything in array:
//filtered: [ { tags: ['books','programming','travel' ]} ]
sift({ tags: {$all: ['books','programming'] }}, [
{ tags: ['books','programming','travel' ] },
{ tags: ['travel','cooking'] } ]);
ability to use an array of expressions. All expressions must test true.
//filtered: [ { name: 'Craig', state: 'MN' }]
sift({ $and: [ { name: 'Craig' }, { state: 'MN' } ] }, [
{ name: 'Craig', state: 'MN' },
{ name: 'Tim', state: 'MN' },
{ name: 'Joe', state: 'CA' } ]);
OR array of expressions.
//filtered: [ { name: 'Craig', state: 'MN' }, { name: 'Tim', state: 'MN' }]
sift({ $or: [ { name: 'Craig' }, { state: 'MN' } ] }, [
{ name: 'Craig', state: 'MN' },
{ name: 'Tim', state: 'MN' },
{ name: 'Joe', state: 'CA' } ]);
opposite of or:
//filtered: [ { name: 'Tim', state: 'MN' }, { name: 'Joe', state: 'CA' }]
sift({ $nor: [ { name: 'Craig' }, { state: 'MN' } ] }, [
{ name: 'Craig', state: 'MN' },
{ name: 'Tim', state: 'MN' },
{ name: 'Joe', state: 'CA' } ]);
Matches an array - must match given size:
//filtered: ['food','cooking']
sift({ tags: { $size: 2 } }, [ { tags: ['food','cooking'] }, { tags: ['traveling'] }]);
Matches a values based on the type
sift({ $type: Date }, [new Date(), 4342, 'hello world']); //returns single date
sift({ $type: String }, [new Date(), 4342, 'hello world']); //returns ['hello world']
Matches values based on the given regular expression
sift({ $regex: /^f/i, $nin: ["frank"] }, ["frank", "fred", "sam", "frost"]); // ["fred", "frost"]
sift({ $regex: "^f", $options: "i", $nin: ["frank"] }, ["frank", "fred", "sam", "frost"]); // ["fred", "frost"]
Matches based on some javascript comparison
sift({ $where: "this.name === 'frank'" }, [{name:'frank'},{name:'joe'}]); // ["frank"]
sift({
$where: function() {
return this.name === "frank"
}
}, [{name:'frank'},{name:'joe'}]); // ["frank"]
Matches elements of array
var bills = [{
month: 'july',
casts: [{
id: 1,
value: 200
},{
id: 2,
value: 1000
}]
},
{
month: 'august',
casts: [{
id: 3,
value: 1000,
}, {
id: 4,
value: 4000
}]
}];
var result = sift({
casts: {$elemMatch:{
value: {$gt: 1000}
}}
}, bills); // {month:'august', casts:[{id:2, value: 1000},{id: 4, value: 4000}]}
Not expression:
sift({$not:{$in:['craig','tim']}}, ['craig','tim','jake']); //['jake']
sift({$not:{$size:5}}, ['craig','tim','jake']); //['tim','jake']
var people = [{
name: 'craig',
address: {
city: 'Minneapolis'
}
},
{
name: 'tim',
address: {
city: 'St. Paul'
}
}];
var sifted = sift({ address: { city: 'Minneapolis' }}, people); // count = 1
//or
var sifted = sift({'address.city': 'minneapolis'}, people);//count = 1
You can add your own expressions. For instance - say you want to do some bitmask filtering, you could add this example:
sift.use({
$band: function(a, b) {
return (a & b) ? 0 : -1; // 0 = exists, -1 = doesn't exist
}
});
// ops
var IS_ANIMAL = 2,
IS_PERSON = IS_ANIMAL << 1,
IS_DOG = IS_PERSON << 1,
EATS_CEREAL = IS_DOG << 1,
EATS_BONES = EATS_CEREAL << 1;
sift({ $band: IS_PERSON }, [ IS_PERSON|EATS_CEREAL, IS_DOG|EATS_BONES, IS_PERSON ]);
Get the index (0-based) of first matching element in target array. Returns -1
if no match is found.
var people = [{
name: 'craig',
address: {
city: 'Minneapolis'
}
},
{
name: 'tim',
address: {
city: 'St. Paul'
}
}];
var index = sift.indexOf({ address: { city: 'Minneapolis' }}, people); // index = 0
FAQs
MongoDB query filtering in JavaScript
The npm package sift receives a total of 1,896,231 weekly downloads. As such, sift popularity was classified as popular.
We found that sift demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.